import { getStore } from '@/libs/storage';
let baseApi = config.baseApi;
async function getImgById(id) {
    const instance = axios.create();
    let accessToken = getStore('accessToken');
    let res = await instance({
        method: 'post',
        url: `${baseApi}/file/getById/`,
        data: 'id=' + id,
        headers: {
            accessToken: accessToken
        },
        responseType: 'arraybuffer'
    });
    let data = btoa(new Uint8Array(res.data).reduce((data, byte) => data + String.fromCharCode(byte), ''));
    return 'data:image/png;base64,' + data;
}

const imgAuth = {
    inserted: function (el, binding) {
        let url = el.attributes['img-url'];
        if (url && typeof url.value === 'string' && url.value.length > 0) {
            // 请求接口
            getImgById(url.value).then(res => {
                el.setAttribute('src', res);
            });
        } else {
            console.warn('没有给图片设置img-url');
        }
    }
};

export default imgAuth;
export {getImgById}